package com.waimai1.waimai.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.waimai1.waimai.common.BaseContext;
import com.waimai1.waimai.common.R;
import com.waimai1.waimai.entity.PageComponent;
import com.waimai1.waimai.service.PageComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (PageComponent)表控制层
 *
 * @author makejava
 * @since 2024-04-30 15:29:47
 */
@RestController
@RequestMapping("pageComponent")
public class PageComponentController extends ApiController {
    @Autowired
    public RedisTemplate<String, String> redisTemplate;
    /**
     * 服务对象
     */
    @Resource
    private PageComponentService pageComponentService;

    /**
     * 获取页面的可编辑内容
     *
     * @param map 页面编号
     * @return 组件列表
     */
   @PostMapping("getPageContent")
    public R<List<PageComponent>> getPageContent(@RequestBody Map<String,Integer> map){
       int acid=BaseContext.getAcid();
       LambdaQueryWrapper<PageComponent> pageComponentLambdaQueryWrapper=new LambdaQueryWrapper<>();
       pageComponentLambdaQueryWrapper.eq(PageComponent::getPageIndex,map.get("pageIndex"))
       .eq(PageComponent::getAcid, BaseContext.getAcid());
       pageComponentLambdaQueryWrapper.orderBy(true,true,PageComponent::getComponentOrder);
       List<PageComponent> res=pageComponentService.list(pageComponentLambdaQueryWrapper);
       for(PageComponent pageComponent:res){
           if("tanchuang".equals(pageComponent.getComponentSort())){
               boolean judgeTanchuang;
               if(!redisTemplate.hasKey(BaseContext.getAcid()+":TanchuangWhite"+pageComponent.getId())){
                   judgeTanchuang=true;
               }else{
                   judgeTanchuang=redisTemplate.opsForSet().isMember(BaseContext.getAcid()+":TanchuangWhite"+pageComponent.getId(),BaseContext.getPhone());
               }
               pageComponent.setTexts(judgeTanchuang?"1":"0");
           }
       }
       return R.success(res);
   }
//   通过名称获取页面元素
    @PostMapping("getPageContentByName")
    public R<PageComponent> getPageContentByName(@RequestBody Map<String,String> map) {
        LambdaQueryWrapper<PageComponent> pageComponentLambdaQueryWrapper=new LambdaQueryWrapper<>();
        pageComponentLambdaQueryWrapper.eq(PageComponent::getComponentSort,map.get("componentSort"));
        PageComponent pageComponent=pageComponentService.getOne(pageComponentLambdaQueryWrapper,false);
        return R.success(pageComponent);
    }
//   获取localshop的类别列表
    @GetMapping("getLocalShopSort")
    public R<PageComponent> getLocalShopSort(){
        LambdaQueryWrapper<PageComponent> pageComponentLambdaQueryWrapper=new LambdaQueryWrapper<>();
        pageComponentLambdaQueryWrapper.eq(PageComponent::getComponentSort,"localshop")
                .eq(PageComponent::getAcid, BaseContext.getAcid());
        pageComponentLambdaQueryWrapper.orderBy(true,true,PageComponent::getComponentOrder);
        PageComponent res=pageComponentService.getOne(pageComponentLambdaQueryWrapper,false);
        return R.success(res);
    }
    //保存弹窗白名单
    @PostMapping("/addTanchuangWhite")
    public R<String> saveYuangongWhite(@RequestBody Map<String,Object> map) {
        List<String> phonesList=(List<String>)map.get("phonesList");
        redisTemplate.opsForSet().add(BaseContext.getAcid()+":TanchuangWhite"+map.get("ComponentId"), phonesList.toArray(new String[0]));
        return R.success("ok");
    }

    //清空弹窗白名单用户集合
    @PostMapping("/clearTanchuangWhite")
    public R<String> clearYuangongWhite(@RequestBody Integer ComponentId) {
        redisTemplate.delete(BaseContext.getAcid()+":TanchuangWhite"+ComponentId);
        return R.success("ok");
    }
}

